//
//  NRWCooperationVC.m
//  New_Runway
//
//  Created by 邢毅 on 2018/5/3.
//  Copyright © 2018年 NelsonJ. All rights reserved.
//

#import "NRWCooperationVC.h"

@interface NRWCooperationVC ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topConstraint;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UIButton *completedBtn;

@end

@implementation NRWCooperationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationBarView.titleLabel.text = @"启辕合作";
    self.topConstraint.constant = self.navH + 25;
    self.view.backgroundColor = getColorFromString(@"F5F5F5");
    self.completedBtn.enabled = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldTextDidChange:) name:UITextFieldTextDidChangeNotification object:nil];
}

- (void)viewDidDisappear:(BOOL)animated {
    
    [super viewDidDisappear:animated];
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)textFieldTextDidChange:(NSNotification *)notification {
    
    if (self.phoneTextField.text.length > 11) {
        
        self.phoneTextField.text = [self.phoneTextField.text substringToIndex:11];
    }
    
    self.completedBtn.enabled = [NRWCarRegularTool isPhoneNum:self.phoneTextField.text];
}

- (IBAction)completedBtnDidClick:(UIButton *)sender {
    
    NSString *url = [NSString stringWithFormat:@"%@%@",Api, insertCooperation];
    NSDictionary *dict = @{@"cooperationMobile": self.phoneTextField.text};
    
    [SVProgressHUD show];
    
    [YQNetworking getWithUrl:url refreshRequest:NO cache:NO params:dict progressBlock:nil successBlock:^(id response) {
        
        [SVProgressHUD dismiss];
        
        if ([response[@"code"] integerValue] == 1) {//成功
            
            [ShowMessage showHintMessage:@"提交成功!" duration:2 withRemindId:remindSuccess];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:YES];
            });
        } else {
            
            [ShowMessage showHintMessage:response[@"msg"] duration:2 withRemindId:remindWarning];
        }
    
    } failBlock:^(NSError *error) {
    
        [ShowMessage showHintMessage:@"请求失败, 请稍后再试" duration:2 withRemindId:remindFail];
        [SVProgressHUD dismiss];
    }];
}

@end
